package org.mxp.demo8;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import org.mxp.utils.RabbitMQUtils;

public class ReceiveObj {
    public static void main(String[] args) throws Exception {
        // 交换机名称
        String EXCHANGE_NAME = "topic_logs";
        // 获取信道
        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机，第一个参数为交换机名称，第二个参数为交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        // 声明Q1队列，并返回队列名称，返回值就是Q1
        String queueName1 = channel.queueDeclare("Q1", false, false, false, null).getQueue();
        // 绑定Q1队列与交换机，路由键为*.orange.*
        channel.queueBind(queueName1, EXCHANGE_NAME, "*.orange.*");
        // Q1接受消息
        channel.basicConsume(queueName1, true, (consumerTag, message) -> {
            System.out.println(queueName1 + "接受到消息：" + new String(message.getBody()));
        }, (consumerTag) -> {});

        // 声明Q2队列，并返回队列名称，返回值就是Q2
        String queueName2 = channel.queueDeclare("Q2", false, false, false, null).getQueue();
        // 绑定Q2队列与交换机，路由键为*.*.rabbit和lazy.#
        channel.queueBind(queueName2, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName2, EXCHANGE_NAME, "lazy.#");
        // 接受消息
        channel.basicConsume(queueName2, true, (consumerTag, message) -> {
            System.out.println(queueName2 + "接受到消息：" + new String(message.getBody()));
        }, (consumerTag) -> {});
    }
}
